//无查询条件的简单CRUD，用于数量较少的表的界面维护

//参数列表：{c:插入URL,r:查询url,u:修改url,d:删除url,grid:表格控件,idName:标识字段名称,translateFun:列表数据翻译}
function SimpleCrudUtil(param){
	this.ALL_LIST = []
	var ths = this;
	
	//检查重复
	this.dupl = function(key){
		for(var i in ALL_LIST){
			if(ALL_LIST[i][param["idName"]] == key){
				return true;
			}
		}
		return false;
	};
	
	//从（表格）中获得条目
	this.getSelectedItem = function(){
		var item = D.getSelectedRowOfTable(param["grid"])[0];
		if(item == null){
			alert("请至少选择表格中的一行");
		}
		return item;
	};
	
	
	//增
	this.C = function(model){
		for(var i in ALL_LIST){
			if(ALL_LIST[i][param["idName"]] == model[param["idName"]]){
				alert("重复，不能新增");
				return;
			}
		}
		dojo.xhrPost({
			 url:param["c"],
			 content:model,
			 encoding : 'UTF-8',
			 handleAs:"text",
			 load:function(response){
				 alert(response);
				 ths.R();
			 }
		});
	};
	
	//删
	this.D = function(){
		var item = D.getSelectedRowOfTable(param["grid"])[0];
		if(item == null){
			alert("请至少选择表格中的一行来删除");
			return;
		}
		var content = {};
		content[param["idName"]] = item[param["idName"]];
		if(!confirm("确认删除吗？"))return;
		
		dojo.xhrGet({
			 url:param["d"],
			 content:content,
			 encoding : 'UTF-8',
			 handleAs:"text",
			 preventCache:true,
			 load:function(response){
				 alert(response);
				 ths.R();
			 }
		});
	};
	
	//改
	this.U = function(model){
		dojo.xhrPost({
			 url:param["u"],
			 content:model,
			 encoding : 'UTF-8',
			 handleAs:"text",
			 load:function(response){
				 alert(response);
				 if(param["r"]){
					 ths.R();
				 }
			 }
		});
	};
	
	//查
	this.R = function(){
		dojo.xhrGet({
			 url:param["r"],
			 handleAs:"json",
			 preventCache:true,
			 load:function(response){
				ALL_LIST = response;
				if(param["translateFun"] != null){
					for(var i in response){
						param["translateFun"](response[i]);
					}
				}
				D.fillTableData(param["grid"],response);
			 }
		});
	};
}